package com.ruoyi.worker.service.impl;

import com.google.common.collect.Maps;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.worker.constant.Constant;
import com.ruoyi.worker.domain.*;
import com.ruoyi.worker.domain.workAttendance.RzScheduleStaffPlan;
import com.ruoyi.worker.mapper.ProfessionRankMapper;
import com.ruoyi.worker.mapper.TermAttendanceGroupMapper;
import com.ruoyi.worker.mapper.TermWorkerMapper;
import com.ruoyi.worker.mapper.WorkerInfoMapper;
import com.ruoyi.worker.service.ITermWorkerService;
import com.ruoyi.worker.service.v2.RzAttendanceSumDailyRecordV2Service;
import com.ruoyi.worker.service.workerSys.ISchedulingTask;
import io.jsonwebtoken.lang.Collections;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 功能描述：
 *
 * @Classname TermWorkerServiceImpl
 * @Description TODO
 * @Date 2021/11/8 14:44
 * @Created by 刘松山
 */
@Service
@Slf4j
public class TermWorkerServiceImpl implements ITermWorkerService {
    @Autowired
    private TermWorkerMapper termWorkerMapper;
    @Autowired
    private TermAttendanceGroupMapper termAttendanceGroupMapper;
    @Autowired
    private ProfessionRankMapper professionRankMapper;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private WorkerInfoMapper workerInfoMapper;
    @Autowired
    private ISchedulingTask schedulingTask;
    @Autowired
    private RzAttendanceSumDailyRecordV2Service rzAttendanceSumDailyRecordV2Service;
    /**
     * 查询【长期工】
     *
     * @param id 【长期工】主键
     * @return 【长期工】
     */
    @Override
    public TermWorker selectTermWorkerById(Long id) {
        return termWorkerMapper.selectTermWorkerById(id);
    }

    /**
     * 查询【长期工】列表
     *
     * @param termWorker 【长期工】
     * @return 【长期工】
     */
    @Override
    public List<TermWorker> selectTermWorkerList(TermWorker termWorker) {
        List<TermWorker> list = termWorkerMapper.selectTermWorkerList(termWorker);
        List<SysDept> deptList = termWorkerMapper.listDept();
        for (TermWorker t : list) {
            if (t.getClazzId() != null) {
                List<String> clazzNameList = new ArrayList<>();
                String[] clazzIdList = t.getClazzId().split(",");
                for (SysDept s : deptList) {
                    for (String c : clazzIdList) {
                        if (c.equals(s.getDeptId() + "")) {
                            clazzNameList.add(s.getDeptName());
                        }
                    }

                }
                if (clazzNameList.size() > 0) {
                    t.setClazzName(String.join(",", clazzNameList));
                }
            }
            for (SysDept s : deptList) {
                if (s.getCategory() == 3) {
                    if (t.getAreaId() != null && t.getAreaId().longValue() == s.getDeptId().longValue()) {
                        t.setAreaName(s.getDeptName());
                    }

                } else if (s.getCategory() == 4) {
                    if (t.getGroupId() != null && t.getGroupId().longValue() == s.getDeptId().longValue()) {
                        t.setGroupName(s.getDeptName());
                    }

                } else {
                    if (t.getPostId() != null && t.getPostId().longValue() == s.getDeptId().longValue()) {
                        t.setPostName(s.getDeptName());
                    }
                }
            }
            // 转换状态值为对应的名称
            this.transform(t);
        }

        return list;
    }

    /**
     * 新增【长期工】
     *
     * @param termWorker 【长期工】
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertTermWorker(TermWorker termWorker) {
        if (termWorker.getWorkStatus() == null) {
            termWorker.setWorkStatus(1);
        }
        termWorker.setStatus(0);
        int count = termWorkerMapper.countTermWorker(termWorker);
        if (count > 0) {
            return -1;
        }
        termWorker.setCreateTime(DateUtils.getNowDate());
        if (termWorker.getComeTime() != null) {
            termWorkerMapper.addTermWorkerLog(termWorker);
        }

        int flag = termWorkerMapper.insertTermWorker(termWorker);

        if (null != termWorker.getImgUrl() && !"".equals(termWorker.getImgUrl())) {
            TermWorkerImg a = new TermWorkerImg();
            a.setCreateBy(termWorker.getCreateBy());
            a.setWorkerId(termWorker.getId());
            a.setImgUrl(termWorker.getImgUrl().substring(termWorker.getImgUrl().indexOf("images") - 1, termWorker.getImgUrl().length()));
            termWorkerMapper.deleteTermWorkerImg(a);
            termWorkerMapper.addTermWorkerImg(a);
        }

        return flag;
    }

    /**
     * 修改【长期工】
     *
     * @param termWorker 【长期工】
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateTermWorker(TermWorker termWorker) throws ParseException {
        int flag = 1;
        int count = termWorkerMapper.countTermWorker(termWorker);
        if (count > 0) {
            return -1;
        }
        termWorker.setUpdateTime(DateUtils.getNowDate());
        termWorker.setCreateTime(new Date());
        //处理离职人员的排班考勤
        if (termWorker.getWorkStatus().equals(0)) {
            Date timeOfTheDay = DateUtils.getTheLastTimeOfTheDay(new Date());
            if (StringUtils.isNull(termWorker.getLeaveTime())) {
                termWorker.setLeaveTime(timeOfTheDay);
            }
            RzScheduleStaffPlan plan = new RzScheduleStaffPlan();
            plan.setStaffId(termWorker.getId()).setScheduleDate(timeOfTheDay);
            int lines = schedulingTask.handleTheSchedulingOfDepartingEmployees(plan);
            log.info("处理离职人员的排班考勤,已删除排班行数为:{}", lines);
        }
        TermWorker t = termWorkerMapper.selectTermWorkerById(termWorker.getId());
        flag = termWorkerMapper.updateTermWorker(termWorker);
        int k = 0;
        if (termWorker.getLeaveTime() != null && !"".equals(termWorker.getLeaveTime())) {
            if (!termWorker.getLeaveTime().equals(t.getLeaveTime())) {
                k = 1;
                flag = termWorkerMapper.addTermWorkerLog(termWorker);
            }
        }
        if (termWorker.getComeTime() != null && !"".equals(termWorker.getComeTime())) {
            if (!termWorker.getComeTime().equals(t.getComeTime())) {
                if (k != 1) {
                    flag = termWorkerMapper.addTermWorkerLog(termWorker);
                }

            }
        }
        termWorkerMapper.deleteTermWorkerGroup(termWorker.getIdCard());

        //没有上传图片 就不修改
        if (null != termWorker.getImgUrl() && !"".equals(termWorker.getImgUrl()) && termWorker.getUplaodImgFlag() == 1) {
            TermWorkerImg a = new TermWorkerImg();
            a.setCreateBy(termWorker.getCreateBy());
            a.setWorkerId(termWorker.getId());
            a.setImgUrl(termWorker.getImgUrl().substring(termWorker.getImgUrl().indexOf("images") - 1, termWorker.getImgUrl().length()));
            termWorkerMapper.deleteTermWorkerImg(a);
            termWorkerMapper.addTermWorkerImg(a);
        }
        // 是否修改长期工信息 有 记流水
        List<TermWorkerModifyLog> termWorkerModifyLogList = this.addTermWorkerModifyLog(termWorker, t);
        if (termWorkerModifyLogList.size() > 0) {
            termWorkerMapper.insertTermWorkerModifyLog(termWorkerModifyLogList);
        }
        //是否加入黑名单
        if (termWorker.getStatus() == 1) {
            RzBlockWorker beau = new RzBlockWorker();
            beau.setWorkerId(termWorker.getAreaId());
            beau.setWorkerName(termWorker.getWorkerName());
            beau.setIdCard(termWorker.getIdCard());
            beau.setAge(termWorker.getAge());
            beau.setPhoneCode(termWorker.getPhoneCode());
            beau.setBlockRemark("从长期工修改页面修改");
            beau.setWorkerType(Constant.WORKER_TYPE_C);
            beau.setCreateTime(DateUtils.getNowDate());
            beau.setCreateBy(SecurityUtils.getUsername());
            termWorkerMapper.addBlackList(beau);
        }
        //调用排班考勤的接口
         if(null !=termWorker.getIdCard() && termWorker.getIdCard().equals(t.getIdCard())){
             rzAttendanceSumDailyRecordV2Service.changeIdCardWhenBaseUpdate(t.getIdCard(), termWorker.getIdCard());
         }

        return 1;
    }

    /**
     * 批量删除【长期工】
     *
     * @param ids 需要删除的【长期工】主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteTermWorkerByIds(Long[] ids) {
        termWorkerMapper.deleteBatchTermWorkerGroupByWorkerId(ids);
        termWorkerMapper.addDeleteBatchTermWorker(ids);
        return termWorkerMapper.deleteTermWorkerByIds(ids);
    }

    /**
     * 删除【长期工】信息
     *
     * @param id 【长期工】主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteTermWorkerById(Long id) {
        Long[] ids = new Long[1];
        ids[0] = id;
        termWorkerMapper.deleteBatchTermWorkerGroupByWorkerId(ids);
        return termWorkerMapper.deleteTermWorkerById(id);
    }

    @Override
    public Map<String, Object> listDept() {
        Map<String, Object> map = new HashMap<>();
        //2-班组，3-区域，4-组别，5-岗位
        List<SysDept> list = termWorkerMapper.listDept();
        List<SysDept> arealist = new ArrayList<>();
        List<SysDept> clazzlist = new ArrayList<>();
        List<SysDept> grouplist = new ArrayList<>();
        List<SysDept> postlist = new ArrayList<>();

        List<List<SysDept>> groupOfList = new ArrayList<>();

        list.stream().collect(Collectors.groupingBy(SysDept::getCategory, Collectors.toList()))
                .forEach((category, deptList) -> {
                    groupOfList.add(deptList);
                });

        clazzlist = groupOfList.get(0);
        arealist = groupOfList.get(1);
        grouplist = groupOfList.get(2);
        postlist = groupOfList.get(3);

        //班组数据
        map.put(Constant.CLAZZ_KEY, clazzlist);
        //区域
        map.put(Constant.AREA_KEY, arealist);
        //组别
        map.put(Constant.GROUP_KEY, grouplist);
        //岗位
        map.put(Constant.POST_KEY, postlist);
        return map;
    }

    @Override
    @Transactional
    public List<String> importTermWorker(List<TermWorkerExcel> excelTermWorkers) {
        return null;

    }

    @Override
    public List<TermWorker> selelctTermWorkerLogList(String idCard) {
        List<TermWorker> list = termWorkerMapper.selelctTermWorkerLogList(idCard);
        for (TermWorker t : list) {
            if (t.getSex() == 1) {
                t.setSexName("男");
            } else {
                t.setSexName("女");
            }
        }

        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateWorkStatus(RzBlockWorker beau) {
        beau.setWorkerType(Constant.WORKER_TYPE_C);
        beau.setCreateTime(DateUtils.getNowDate());
        beau.setCreateBy(SecurityUtils.getUsername());
        termWorkerMapper.addBlackList(beau);
        return termWorkerMapper.updateWorkerStatus(beau.getWorkerId(), Constant.WORKER_STATUS_BLOCK_C);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addTermWorkerImg(TermWorkerImg termWorkerImg) {
        termWorkerMapper.deleteTermWorkerImg(termWorkerImg);
        termWorkerMapper.addTermWorkerImg(termWorkerImg);
        return 1;
    }

    /**
     * 获取职级数据
     *
     * @return java.util.Map
     * @author Hush.
     * @since 2021/11/28 16:53
     */
    @Override
    public List<Map<String, Object>> listProfessionRank() {
        List<ProfessionRank> ranks = professionRankMapper.getProfessionRankListInfo(new ProfessionRank());
        return ranks.stream().map(m -> {
            Map<String, Object> map = Maps.newHashMap();
            map.put("key", m.getId());
            map.put("value", m.getRankName());
            return map;
        }).collect(Collectors.toList());
    }

    /**
     * 长期工模板中的校验数据(缓存)
     *
     * @return java.util.Map
     * @author Hush.
     * @since 2021/12/2 15:28
     */
    @Override
    public Map<String, Object> cashDataForTermWorkerExcel() {
        String cashKey = "cashData::termWorker::excel";
        Map<String, Object> cacheMap = redisCache.getCacheMap(cashKey);
        if (!Collections.isEmpty(cacheMap)) {
            return cacheMap;
        }
        Map<String, Object> result = Maps.newHashMap();
        //组织结构列表
        Map<String, Object> cashMap = this.listDept();
        for (Map.Entry<String, Object> entry : cashMap.entrySet()) {
            Map<String, Object> temp = Maps.newHashMap();
            List<SysDept> value = (List<SysDept>) entry.getValue();
            for (SysDept dept : value) {
                temp.put(dept.getDeptName(), dept.getDeptId());
            }
            result.put(entry.getKey(), temp);
        }
        //职级列表
        List<Map<String, Object>> professionRank = this.listProfessionRank();
        Map<String, Object> professionTemp = Maps.newHashMap();
        for (Map<String, Object> map : professionRank) {
            professionTemp.put(map.get("value").toString(), map.get("key"));
        }
        result.put(Constant.PROFESSION_KEY, professionTemp);

        Map<String, Object> workStatusTemp = Maps.newHashMap();
        workStatusTemp.put("离职", 0);
        workStatusTemp.put("在职", 1);
        result.put(Constant.WORK_STATUS_KEY, workStatusTemp);
        Map<String, Object> salaryMethodTemp = Maps.newHashMap();
        salaryMethodTemp.put("班次*单价", 0);
        salaryMethodTemp.put("计件制", 1);
        result.put(Constant.SALARY_METHOD_KEY, salaryMethodTemp);
        Map<String, Object> jobStatusTemp = Maps.newHashMap();
        jobStatusTemp.put("试岗期", 0);
        jobStatusTemp.put("试用期", 1);
        jobStatusTemp.put("正式工", 2);
        result.put(Constant.JOB_STATUS_KEY, jobStatusTemp);

        redisCache.setCacheMap(cashKey, result, 200);
        return result;
    }

    /**
     * 导入长期工excel模板
     *
     * @param list : excel数据
     * @author Hush.
     * @since 2021/12/2 17:39
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importTermWorkerTemplate(List<TermWorkerExcel> list) {
        //保存(更新)长期工
        List<TermWorkerExcel> insertList = new ArrayList<>();
        List<TermWorkerExcel> updateList = new ArrayList<>();
        //1.检查数据库有无存在的长期工信息
        for (TermWorkerExcel data : list) {
            Long id = termWorkerMapper.selectTermWorkerIdByIdCard(data.getIdCard());
            if (StringUtils.isNull(id)) {
                data.setCreateTime(DateUtils.getNowDate());
                insertList.add(data);
            } else {
                data.setUpdateTime(DateUtils.getNowDate());
                updateList.add(data);
            }
        }

        int insert = 0;
        int update = 0;
        if (!CollectionUtils.isEmpty(insertList)) {
            insert = termWorkerMapper.addTermWorkerList(insertList);
        }
        if (!CollectionUtils.isEmpty(updateList)) {
            update = termWorkerMapper.updateTermWorkerList(updateList);
        }
        log.info(String.format("保存(更新)长期工excel模板,更新:%s,新增:%s,总量:%s", update, insert, list.size()));

    }

    /**
     * 检查是否已在黑名单中
     *
     * @param beau:黑名单对象
     * @return RzBlockWorker
     * @author Hush.
     * @since 2021/12/15 15:46
     */
    @Override
    public RzBlockWorker checkBlackFlag(RzBlockWorker beau) {
        return termWorkerMapper.selectRzBlockWorker(beau.getWorkerId(), beau.getWorkerType());
    }

    /**
     * 黑名单员工分页数据
     *
     * @param dto : 检索条件
     * @return com.ruoyi.common.core.page.TableDataInfo
     * @author Hush.
     * @since 2021/12/15 17:16
     */
    @Override
    public List<RzBlockWorker> selectRzBlockWorkerList(RzBlockWorker dto) {
        return termWorkerMapper.selectRzBlockWorkerList(dto);
    }

    /**
     * 移出黑名单
     *
     * @param beau :员工信息
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author Hush.
     * @since 2021/12/16 15:09
     */
    @Override
    public int removeFromBlacklist(RzBlockWorker beau) {
        if (Objects.equals(beau.getWorkerType(), Constant.WORKER_TYPE_C)) {
            //长期工
            termWorkerMapper.updateWorkerStatus(beau.getWorkerId(), Constant.WORKER_STATUS_NO_BLOCK_C);
        }
        if (Objects.equals(beau.getWorkerType(), Constant.WORKER_TYPE_L)) {
            //临时工
            workerInfoMapper.updateWorkerStatus(beau.getWorkerId(), Constant.WORKER_STATUS_NO_BLOCK_L);
        }
        return termWorkerMapper.deleteBlackByWidAndWType(beau.getWorkerId(), beau.getWorkerType());
    }

    @Override
    public List<TermWorkerModifyLog> selectTermWorkerModifyLogById(TermWorkerModifyLog t) {

        return termWorkerMapper.selectTermWorkerModifyLogById(t);
    }

    @Override
    public int updateTermWorkerLeaveTime(Long[] ids, String userName) {
        termWorkerMapper.updateTermWorkerLeaveTime(ids);
        //记录修改流水 员工状态
        List<TermWorker> list = termWorkerMapper.listTermWorkerById(ids);
        List<TermWorkerModifyLog> resultList = new ArrayList<>();
        List<TermWorkerLog> resultLogList = new ArrayList<>();
        for (TermWorker t : list) {
            TermWorkerModifyLog termWorkerModifyLog = this.getTermWorkerModifyLog("员工状态", t);
            termWorkerModifyLog.setCreateBy(userName);
            termWorkerModifyLog.setNewResult("离职");
            termWorkerModifyLog.setOldResult("在职");
            resultList.add(termWorkerModifyLog);

            TermWorkerLog termWorkerLog =this.getTermWorkerLog(t);
            termWorkerLog.setLeaveTime(new Date());
            resultLogList.add(termWorkerLog);

        }
        if (resultList.size() > 0) {
            termWorkerMapper.insertTermWorkerModifyLog(resultList);
        }
        //离职流水
        if (resultLogList.size() > 0) {
            termWorkerMapper.addTermWorkerLogList(resultLogList);
        }

        return 1;

    }

    public void transform(TermWorker t) {

        if (t.getWorkStatus() == 0) {
            t.setWorkStatusName("离职");
        } else {
            t.setWorkStatusName("在职");
        }
        if (t.getStatus() == 0) {
            t.setStatusName("否");
        } else {
            t.setStatusName("是");
        }
        if (t.getSex() == 1) {
            t.setSexName("男");
        } else {
            t.setSexName("女");
        }
        //0 试岗期 1 试用期  2 正式工
        if (t.getJobStatus() != null) {
            if (t.getJobStatus() == 0) {
                t.setJobStatusName("试岗期");
            } else if (t.getJobStatus() == 1) {
                t.setJobStatusName("试用期");
            } else {
                t.setJobStatusName("正式工");
            }
        }
        //0 班次*单价 1 计件制
        if (t.getSalaryMethod() != null) {
            if (t.getSalaryMethod() == 0) {
                t.setSalaryMethodName("班次*单价");
            } else {
                t.setSalaryMethodName("计件制");
            }
        }
    }

    public List<TermWorkerModifyLog> addTermWorkerModifyLog(TermWorker termWorker, TermWorker termWorkerData) {

        //长期工 班组 岗位 转正时间  在职状态 试用期系数 薪资方式 员工状态 职级 修改了 需要记录流水
        List<TermWorkerModifyLog> resultList = new ArrayList<>();
        //1 班组
        if (!termWorker.getClazzId().equals(termWorkerData.getClazzId())) {
            TermWorkerModifyLog termWorkerModifyLog = this.getTermWorkerModifyLog("班组", termWorker);
            List<SysDept> deptList = termWorkerMapper.listDept();

            List<String> clazzNameNewList = new ArrayList<>();
            List<String> clazzNameOldList = new ArrayList<>();
            String[] clazzIdList = termWorker.getClazzId().split(",");
            String[] clazzIdOldList = termWorkerData.getClazzId().split(",");
            for (SysDept s : deptList) {
                for (String c : clazzIdList) {
                    if (c.equals(s.getDeptId().longValue() + "")) {
                        clazzNameNewList.add(s.getDeptName());
                    }
                }
                for (String c : clazzIdOldList) {
                    if (c.equals(s.getDeptId().longValue() + "")) {
                        clazzNameOldList.add(s.getDeptName());
                    }
                }
                termWorkerModifyLog.setNewResult(String.join(",", clazzNameNewList));
                termWorkerModifyLog.setOldResult(String.join(",", clazzNameOldList));
            }


            resultList.add(termWorkerModifyLog);
        }

        //2 岗位
        if (termWorker.getPostId().longValue() != termWorkerData.getPostId().longValue()) {
            TermWorkerModifyLog termWorkerModifyLog = this.getTermWorkerModifyLog("岗位", termWorker);
            List<SysDept> deptList = termWorkerMapper.listDept();
            int i = 0;
            for (SysDept s : deptList) {
                if (s.getCategory() == 5 && termWorker.getPostId().longValue() == s.getDeptId().longValue()) {
                    termWorkerModifyLog.setNewResult(s.getDeptName());
                    i++;
                }
                if (s.getCategory() == 5 && termWorkerData.getPostId().longValue() == s.getDeptId().longValue()) {
                    termWorkerModifyLog.setOldResult(s.getDeptName());
                    i++;
                }
                if (i == 2) {
                    break;
                }
            }
            resultList.add(termWorkerModifyLog);
        }

        //3 转正时间

        if (termWorker.getRegularTime() != null) {
            if (!termWorker.getRegularTime().equals(termWorkerData.getRegularTime())) {
                TermWorkerModifyLog termWorkerModifyLog = this.getTermWorkerModifyLog("转正时间", termWorker);
                termWorkerModifyLog.setNewResult(DateUtils.parseDateToStr("yyyy-MM-dd", termWorker.getRegularTime()));
                if (termWorkerData.getRegularTime() != null) {
                    termWorkerModifyLog.setOldResult(DateUtils.parseDateToStr("yyyy-MM-dd", termWorkerData.getRegularTime()));
                } else {
                    termWorkerModifyLog.setOldResult(null);
                }

                resultList.add(termWorkerModifyLog);
            }
        } else {
            if (termWorkerData.getRegularTime() != null) {
                if (!termWorkerData.getRegularTime().equals(termWorker.getRegularTime())) {
                    TermWorkerModifyLog termWorkerModifyLog = this.getTermWorkerModifyLog("转正时间", termWorker);
                    termWorkerModifyLog.setNewResult(null);
                    termWorkerModifyLog.setOldResult(DateUtils.parseDateToStr("yyyy-MM-dd", termWorkerData.getRegularTime()));
                    resultList.add(termWorkerModifyLog);
                }
            }
        }

        //4 在职状态
        Map<Integer, Object> jobStatusTemp = Maps.newHashMap();
        jobStatusTemp.put(0, "试岗期");
        jobStatusTemp.put(1, "试用期");
        jobStatusTemp.put(2, "正式工");

        if (termWorker.getJobStatus() != null) {
            if (termWorker.getJobStatus() != termWorkerData.getJobStatus()) {
                TermWorkerModifyLog termWorkerModifyLog = this.getTermWorkerModifyLog("在职状态", termWorker);
                termWorkerModifyLog.setNewResult(jobStatusTemp.get(termWorker.getJobStatus()) + "");
                if (termWorkerData.getJobStatus() != null) {
                    termWorkerModifyLog.setOldResult(jobStatusTemp.get(termWorkerData.getJobStatus()) + "");
                } else {
                    termWorkerModifyLog.setOldResult(null);
                }

                resultList.add(termWorkerModifyLog);
            }

        } else {
            if (termWorkerData.getJobStatus() != null) {
                TermWorkerModifyLog termWorkerModifyLog = this.getTermWorkerModifyLog("在职状态", termWorker);
                termWorkerModifyLog.setNewResult(null);
                termWorkerModifyLog.setOldResult(jobStatusTemp.get(termWorkerData.getJobStatus()) + "");
                resultList.add(termWorkerModifyLog);
            }
        }


        //5  试用期系数
        if (termWorker.getProbationRate() == null) {
            termWorker.setProbationRate(0.8);
        }

        if (termWorker.getProbationRate().doubleValue() != termWorkerData.getProbationRate().doubleValue()) {
            TermWorkerModifyLog termWorkerModifyLog = this.getTermWorkerModifyLog("试用期系数", termWorker);
            termWorkerModifyLog.setNewResult(termWorker.getProbationRate() + "");
            termWorkerModifyLog.setOldResult(termWorkerData.getProbationRate() + "");
            resultList.add(termWorkerModifyLog);
        }

        //6 薪资方式
        Map<Integer, Object> salaryMethodTemp = Maps.newHashMap();
        salaryMethodTemp.put(0, "班次*单价");
        salaryMethodTemp.put(1, "计件制");
        if (termWorker.getSalaryMethod() != null) {
            if (termWorker.getSalaryMethod() != termWorkerData.getSalaryMethod()) {
                TermWorkerModifyLog termWorkerModifyLog = this.getTermWorkerModifyLog("薪资方式", termWorker);
                termWorkerModifyLog.setNewResult(salaryMethodTemp.get(termWorker.getSalaryMethod()) + "");
                if (termWorkerData.getSalaryMethod() != null) {
                    termWorkerModifyLog.setOldResult(salaryMethodTemp.get(termWorkerData.getSalaryMethod()) + "");
                } else {
                    termWorkerModifyLog.setOldResult(null);
                }

                resultList.add(termWorkerModifyLog);
            }

        } else {
            if (termWorkerData.getSalaryMethod() != null) {
                TermWorkerModifyLog termWorkerModifyLog = this.getTermWorkerModifyLog("薪资方式", termWorker);
                termWorkerModifyLog.setNewResult(null);
                termWorkerModifyLog.setOldResult(salaryMethodTemp.get(termWorkerData.getSalaryMethod()) + "");
                resultList.add(termWorkerModifyLog);
            }
        }

        //7 员工状态
        Map<Integer, Object> statusMap = Maps.newHashMap();
        statusMap.put(0, "离职");
        statusMap.put(1, "在职");

        if (termWorker.getWorkStatus().intValue() != termWorkerData.getWorkStatus().intValue()) {
            TermWorkerModifyLog termWorkerModifyLog = this.getTermWorkerModifyLog("员工状态", termWorker);
            termWorkerModifyLog.setNewResult(statusMap.get(termWorker.getWorkStatus()) + "");
            if (termWorkerData.getStatus() != null) {
                termWorkerModifyLog.setOldResult(statusMap.get(termWorkerData.getWorkStatus()) + "");
            } else {
                termWorkerModifyLog.setOldResult(null);
            }

            resultList.add(termWorkerModifyLog);
        }


        // 8 职级
        List<Map<String, Object>> professionRank = this.listProfessionRank();
        if (termWorker.getProfessionId() != null) {
            if (termWorker.getProfessionId() != termWorkerData.getProfessionId()) {
                TermWorkerModifyLog termWorkerModifyLog = this.getTermWorkerModifyLog("职级", termWorker);
                if (termWorkerData.getProfessionId() != null) {
                    for (Map<String, Object> map : professionRank) {
                        if (Integer.parseInt(map.get("key") + "") == termWorker.getProfessionId()) {
                            termWorkerModifyLog.setNewResult(map.get("value") + "");
                        }
                        if (Integer.parseInt(map.get("key") + "") == termWorkerData.getProfessionId()) {
                            termWorkerModifyLog.setOldResult(map.get("value") + "");
                        }
                        if (termWorkerModifyLog.getOldResult() != null && termWorkerModifyLog.getNewResult() != null) {
                            break;
                        }

                    }
                } else {
                    for (Map<String, Object> map : professionRank) {
                        if (Integer.parseInt(map.get("key") + "") == termWorker.getProfessionId()) {
                            termWorkerModifyLog.setNewResult(map.get("value") + "");
                        }
                    }
                    termWorkerModifyLog.setOldResult(null);
                }

                resultList.add(termWorkerModifyLog);

            }
        } else {
            if (termWorkerData.getProfessionId() != null) {
                TermWorkerModifyLog termWorkerModifyLog = this.getTermWorkerModifyLog("职级", termWorker);
                termWorkerModifyLog.setNewResult(null);
                for (Map<String, Object> map : professionRank) {

                    if (Integer.parseInt(map.get("key") + "") == termWorkerData.getProfessionId()) {
                        termWorkerModifyLog.setOldResult(map.get("value") + "");
                        break;
                    }

                }

                resultList.add(termWorkerModifyLog);
            }

        }
        return resultList;

    }

    public TermWorkerModifyLog getTermWorkerModifyLog(String title, TermWorker termWorker) {
        TermWorkerModifyLog termWorkerModifyLog = new TermWorkerModifyLog();
        termWorkerModifyLog.setModifyContent(title);
        termWorkerModifyLog.setWorkerId(termWorker.getId());
        termWorkerModifyLog.setWorkerName(termWorker.getWorkerName());
        termWorkerModifyLog.setIdCard(termWorker.getIdCard());
        termWorkerModifyLog.setCreateBy(termWorker.getUpdateBy());
        termWorkerModifyLog.setCreateTime(new Date());
        return termWorkerModifyLog;
    }
    /**
     * @description:  组装入职流水实体类
     * @author 刘松山
     * @date 2022/1/6 13:23
     *
     */
    public TermWorkerLog getTermWorkerLog( TermWorker termWorker) {
        TermWorkerLog termWorkerLog = new TermWorkerLog();
        termWorkerLog.setWorkerName(termWorker.getWorkerName());
        termWorkerLog.setIdCard(termWorker.getIdCard());
        termWorkerLog.setCreateBy(termWorker.getUpdateBy());
        termWorkerLog.setCreateTime(new Date());
        termWorkerLog.setComeTime(termWorker.getComeTime());
        return termWorkerLog;
    }

}
